
exp1 <- read_csv("Data/Conjoint - Statecraft_competence - 1_April 22, 2021_22.csv")


names(exp1)[1] <- "UK_fav"
names(exp1)[2] <- "Canada_fav"
names(exp1)[3] <- "India_fav"
names(exp1)[4] <- "China_fav"
names(exp1)[5] <- "Egypt_fav"
names(exp1)[6] <- "SoAf_fav"
names(exp1)[7] <- "Russia_fav"
names(exp1)[8] <- "NoKor_fav"
names(exp1)[9] <- "Argentina_fav"
names(exp1)[10] <- "Pakistan_fav"
names(exp1)[11] <- "Iran_fav"
names(exp1)[12] <- "Syria_fav"
names(exp1)[13] <- "Cuba_fav"
names(exp1)[14] <- "Sudan_fav"
names(exp1)[15] <- "Mexico_fav"
names(exp1)[16] <- "Colombia_fav"
names(exp1)[17] <- "Myanmar_fav"
names(exp1)[18] <- "Afghanistan_fav"
names(exp1)[19] <- "Morocco_fav"

exp1 <- exp1 %>%
  mutate(party = ifelse(Q52 == "Neither", "TrueInd", 
                        ifelse(Q52 == "Lean Republican", "IndRep",
                               ifelse(Q52 == "Lean Democrat", "IndDem", NA))))
         
exp1$party[exp1$Q50 == "Not very strong"] <- "WeakRep"
exp1$party[exp1$Q50 == "Very strong"] <- "StrongRep"
exp1$party[exp1$Q48 == "Not very strong"] <- "WeakDem"
exp1$party[exp1$Q48 == "Very strong"] <- "StrongDem"

exp1$Q50 <- exp1$Q52 <- exp1$Q48 <- exp1$Q46 <- NULL


exp1 <- exp1 %>%
  rename(urban_rural = Q118)

exp1 <- exp1 %>%
  rename(support_nukes = '1_Q181_4',
         support_dem = '1_Q147_4',
         USint_nukes = '1_Q181_5',
         USint_dem = '1_Q147_5',
         costly_nukes = '1_Q181_6',
         costly_dem = '1_Q147_6',
         effective_nukes = '1_Q181_7',
         effective_dem = '1_Q147_10',
         strength_nukes = '1_Q181_10',
         stength_dem = '1_Q147_11',
         message_nukes = '1_Q181_11',
         message_dem = '1_Q147_12')
                                                    
exp1 <- exp1 %>%
  mutate(DV_support = ifelse(badbehavior1 == "developnukes", support_nukes, 
                             ifelse(badbehavior1 == "stopdemocracy", support_dem, NA)),
         DV_interests = ifelse(badbehavior1 == "developnukes", USint_nukes, 
                             ifelse(badbehavior1 == "stopdemocracy", USint_dem, NA)),
         DV_costly = ifelse(badbehavior1 == "developnukes", costly_nukes, 
                             ifelse(badbehavior1 == "stopdemocracy", costly_dem, NA)),
         DV_effective = ifelse(badbehavior1 == "developnukes", effective_nukes, 
                             ifelse(badbehavior1 == "stopdemocracy", effective_dem, NA)),
         DV_strength = ifelse(badbehavior1 == "developnukes", strength_nukes, 
                             ifelse(badbehavior1 == "stopdemocracy", stength_dem, NA)),
         DV_message = ifelse(badbehavior1 == "developnukes", message_nukes, 
                             ifelse(badbehavior1 == "stopdemocracy", message_dem, NA))
         )

# Final DVs
exp1 <- exp1 %>%
  mutate(
DV_support = as.numeric(DV_support),
DV_interests = as.numeric(DV_interests),
DV_costly = as.numeric(DV_costly),
DV_effective = as.numeric(DV_effective),
DV_strength = as.numeric(DV_strength),
DV_message = as.numeric(DV_message)
  )

exp1 <- exp1 %>%
  select(-support_nukes, -support_dem, -USint_nukes, -USint_dem, -costly_nukes,
         -costly_dem, -effective_nukes, -effective_dem, -strength_nukes,
         -stength_dem, -message_nukes, -message_dem)


# Use text pulls to code IVs
# effect on economy (value)
# rationale
# inaction outcome
# author

exp1 <- exp1 %>%
  mutate(
    Effect = ifelse(c1_attrib1_name == "Effect on economy", word(c1_attrib1, 1), 
                    ifelse(c1_attrib2_name == "Effect on economy", word(c1_attrib2, 1),
                           ifelse(c1_attrib3_name == "Effect on economy", word(c1_attrib3, 1),
                                  ifelse(c1_attrib4_name == "Effect on economy", word(c1_attrib4, 1), NA
                    )))),
    Effect = as.numeric(str_remove(Effect, "[$]"))
  )

exp1 <- exp1 %>%
  mutate(
    Inaction = ifelse(c1_attrib1_name == "Outcome of Inaction", word(c1_attrib1, -1), 
                    ifelse(c1_attrib2_name == "Outcome of Inaction", word(c1_attrib2, -1),
                         ifelse(c1_attrib3_name == "Outcome of Inaction", word(c1_attrib3, -1),
                                ifelse(c1_attrib4_name == "Outcome of Inaction", word(c1_attrib4, -1), NA
                    )))),
  Inaction = ifelse(Inaction == "continue", "Continue", 
                    ifelse(Inaction == "future", "TickingClock", NA))
)

exp1 <- exp1 %>%
  mutate( 
    Rationale = ifelse(c1_attrib1_name == "US Rationale for Proposal", word(c1_attrib1, -1),
                       ifelse(c1_attrib2_name == "US Rationale for Proposal", word(c1_attrib2, -1),
                              ifelse(c1_attrib3_name == "US Rationale for Proposal", word(c1_attrib3, -1),
                                     ifelse(c1_attrib4_name == "US Rationale for Proposal", word(c1_attrib4, -1), NA
                    )))),
    Rationale = ifelse(Rationale == "time", "LongRun", 
                       ifelse(Rationale == "activists" | Rationale == "program", "Immediate", NA))
  )


exp1 <- exp1 %>%
  mutate( 
    Author = ifelse(c1_attrib1_name == "Proposal Author", word(c1_attrib1, -1),
                       ifelse(c1_attrib2_name == "Proposal Author", word(c1_attrib2, -1),
                              ifelse(c1_attrib3_name == "Proposal Author", word(c1_attrib3, -1),
                                     ifelse(c1_attrib4_name == "Proposal Author", word(c1_attrib4, -1), NA
                                     )))),
    Author = ifelse(Author == "appointee", "Appointee_only",
                    ifelse(Author == "donor", "Appointee_donor",
                           ifelse(Author == "service", "Professional",
                                  ifelse(Author == "China" | Author == "India" | Author == "Korea" | Author == "Russia" | Author == "Pakistan", "Professional_expert", NA
                                     ))))
  )


exp1 <- exp1 %>%
  select(-c1_attrib1_name, -c1_attrib2_name, -c1_attrib3_name, -c1_attrib4_name, 
         -c1_attrib1, -c1_attrib2, -c1_attrib3, -c1_attrib4)



exp1 <- exp1 %>%
  mutate(Background = ifelse(countrybackground == "has a long history of repressing pro-democracy activists" | 
                               countrybackground == "has a long history of research and development on weapons of mass destruction (WMD)", "LongHistory",
                             ifelse(countrybackground == "recently shifted policy, leading to a surge in new research and development on weapons of mass destruction (WMD)" |
                                      countrybackground == "recently shifted policy, leading to a surge in repression of pro-democracy activists", "RecentShift", NA)))


# Code policy types
exp1 <- exp1 %>%
  mutate( 
     Policy = ifelse(policy == "Financial sanctions prohibiting certain international payments to and from", "Sanctions", 
                     ifelse(policy == "Financial inducements facilitating investment in", "Inducements", NA))
  )

exp1 <- exp1 %>%
  select(-policy, -policy2, -policy3, -countrybackground, -outcome, -number)



# Code prior support
exp1 <- exp1 %>%
  mutate(
    Prior = ifelse(country1 == "China", China_fav, 
                   ifelse(country1 == "India", India_fav,
                          ifelse(country1 == "North Korea", NoKor_fav, 
                                 ifelse(country1 == "Pakistan", Pakistan_fav,
                                        ifelse(country1 == "Russia", Russia_fav, NA)))))
  )


# Delete unneeded rows
exp1 = exp1[-c(1, 2),]


